/*
 * @Author: hanxin 
 * @Date: 2021-08-30 15:09:32 
 * @Last Modified by: hanxin
 * @Last Modified time: 2021-08-31 01:03:23
 * 简单的界面
 */
package mhl.view;

import java.util.List;

import mhl.javabean.Employee;
import mhl.javabean.bill;
import mhl.javabean.diningTable;
import mhl.javabean.menu;
import mhl.javabean.muliTablebean_b_m;
import mhl.service.billService;
import mhl.service.diningTableService;
import mhl.service.empService;
import mhl.service.menuService;
import mhl.util.utility;

public class mhlView {

    private boolean loop = true;
    private empService empService = new empService();
    private diningTableService diningTableService = new diningTableService();
    private menuService menuService = new menuService();
    private billService billService = new billService();

    public static void main(String[] args) {
        new mhlView().mhlmenu();
        // new mhlView().test();
    }

    public void test() {
        System.out.println(diningTableService.isTableBooking(1));
    }

    // 客户端接收返回信息(diningTable javabean list)并输出
    public void showTables() {
        List<diningTable> tables = diningTableService.getTable();
        for (diningTable diningTable : tables) {
            System.out.println("餐桌号:" + diningTable.getTable_id() + "\t状态:" + diningTable.getState() + "\t订餐人:"
                    + diningTable.getOrderName() + "\t订餐人电话:" + diningTable.getOrderPhone());
        }

        System.out.println();
    }

    public void Booking() {
        char c = utility.readConfirmSelection();
        if (c == 'Y') {
            System.out.println("请输入您要预订的餐桌号");
            Integer tid = utility.readInt();

            if (diningTableService.isTableExist(tid) == null) {
                System.out.println("该餐桌不存在");
                return;
            }
            if (!diningTableService.isTableBooking(tid)) {
                System.out.println("该餐桌已被预订");
                return;
            }
            System.out.println("预订人姓名:");
            String oname = utility.readString(32);
            System.out.println("预订人电话");
            String phone = utility.readString(15);

            if (diningTableService.orderTable(tid, oname, phone)) {
                System.out.println("预订成功");
                return;
            } else {
                System.out.println("预订失败");
                return;
            }

        } else {
            System.out.println("\t退出预订");
            return;
        }

    }

    public void Unsubscribe() {
        System.out.print("是否确认退订(Y/N)");
        String c = utility.readString(1);
        if (c.equals("N") || c.equals("n")) {
            return;

        } else if (c.equals("Y") || c.equals("y")) {
            System.out.println("请输入您要退订的餐桌号");
            Integer tid = utility.readInt();
            if (diningTableService.isTableExist(tid) == null) {
                System.out.println("该餐桌不存在");
                return;
            }

            if (diningTableService.tableState(tid)) {
                System.out.println("该餐桌用餐中");
                return;
            }

            if (diningTableService.UnsubscribeTable(tid)) {
                System.out.println("退订成功");
                return;
            } else {
                System.out.println("退订失败");
                return;
            }
        } else {
            System.out.println("输入错误");
            return;
        }
    }

    public void showmenu() {
        List<menu> list = menuService.showMenu();

        for (menu menu : list) {
            System.out.println("编号" + menu.getId() + "\t菜名:" + menu.getName() + "\t类别:" + menu.getType() + "\t价格:"
                    + menu.getPrice());

        }

    }

    public void orderMenu() {
        System.out.println("请输入菜品编号(输入-1退出点菜):");
        int menuid = utility.readInt();
        if (menuid == -1) {
            System.out.println("退出点菜");
            return;
        }

        // 判断菜品编号是否存在
        if (menuService.getMenuId(menuid) == null) {
            System.out.println("菜品编号不存在");
            return;
        }

        System.out.println("请输入数量(输入-1退出点菜):");
        int nums = utility.readInt();
        if (nums == -1) {
            System.out.println("退出点菜");
            return;
        }

        System.out.println("请输入桌号(输入-1退出点菜):");
        int table_id = utility.readInt();
        if (table_id == -1) {
            System.out.println("退出点菜");
            return;
        }

        // 判断桌号是否存在
        if (diningTableService.isTableExist(table_id) == null) {
            System.out.println("餐桌号不存在");
            return;
        }

        if (billService.orderMenu(menuid, nums, table_id)) {
            System.out.println("点单成功");
        } else {
            System.out.println("点单失败");
        }
    }

    public void showBill() {
        System.out.println("请输入要查看账单的桌号");
        int Tabld_id = utility.readInt();

        List<bill> list = billService.showBill(Tabld_id);
        Double totalmoney = 0.0;
        for (bill bill : list) {

            System.out.println("菜品:" + menuService.getMenuId(bill.getMenuID()).getName() + "\t份数:" + bill.getNums()
                    + "\t金额" + bill.getMoney() + "\t桌号" + bill.getDiningTableID() + "\t订单日期" + bill.getBillDate()
                    + "\t状态:" + bill.getState());

            totalmoney += bill.getMoney();
        }
        System.out.println("合计: " + totalmoney);
    }

    public void showBill_Total() {
        List<muliTablebean_b_m> list = billService.showBill2();
        for (muliTablebean_b_m muliTablebean_b_m : list) {
            System.out.println("菜品:" + muliTablebean_b_m.getName() + "\t份数:" + muliTablebean_b_m.getNums() + "\t金额:"
                    + muliTablebean_b_m.getMoney() + "\t桌号" + muliTablebean_b_m.getDiningTableID() + "\t订单日期:"
                    + muliTablebean_b_m.getBillDate() + "\t状态:" + muliTablebean_b_m.getState());
        }
    }

    public void paybill() {
        System.out.println("请输入要结账的桌号(输入-1退出)");
        int table = utility.readInt();

        if (table == -1) {
            return;
        }
        if (diningTableService.isTableExist(table) == null) {
            System.out.println("该餐桌不存在");
            return;
        }
        if (billService.unPaydiningTable(table)) {
            System.out.println("请输入支付方式");
            String mode = utility.readString(10);

            if (billService.paying(table, mode)) {
                System.out.println("支付成功");
            } else {
                System.out.println("支付失败");
            }

        } else {
            System.out.println("该餐桌无账单");
        }
    }

    public void mhlmenu() {
        while (loop) {
            System.out.println("输入1登录系统");
            System.out.print("输入2退出系统\n");

            System.out.print("请输入:");

            String key = utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("请输入用户名:");
                    String id = utility.readString(50);
                    System.out.print("请输入密码:");
                    String pwd = utility.readString(50);

                    Employee employee = empService.getEmpidAndPwd(id, pwd);
                    if (employee != null) {
                        System.out.println(employee.getEname() + " login_success");

                        boolean loop = true;
                        while (loop) {
                            System.out.println();

                            System.out.println("\t输入1 显示餐桌状态");
                            System.out.println("\t输入2 预定餐桌");
                            System.out.println("\t输入3 退订餐桌");
                            System.out.println("\t输入4 显示所有菜品");
                            System.out.println("\t输入5 点餐服务");
                            System.out.println("\t输入6 查看账单");
                            System.out.println("\t输入7 查看所有账单");
                            System.out.println("\t输入8 结账");
                            System.out.println("\t输入9 退出登录");

                            System.out.print("请输入:");

                            String key2 = utility.readString(1);
                            switch (key2) {
                                case "1":
                                    showTables();
                                    break;
                                case "2":
                                    Booking();
                                    break;
                                case "3":
                                    Unsubscribe();
                                    break;

                                case "4":
                                    showmenu();
                                    break;

                                case "5":
                                    orderMenu();

                                    break;
                                case "6":
                                    showBill();
                                    break;
                                case "7":
                                    showBill_Total();
                                    break;
                                case "8":
                                    paybill();
                                    break;
                                case "9":
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("输入有误");
                                    break;
                            }
                        }
                    } else {
                        System.out.println("fail");
                    }
                    break;

                case "2":
                    System.out.println("\n");
                    System.out.println("exit");
                    loop = false;

                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }
}
